<template>
    <div class="container">
        <div class="head">
            <div class="img">
                <img src="../../assets/NJUST.jpeg" />
            </div>
            <div class="person">
                <img src="../../assets/用户名.png" />
                <li class="li1"><router-link to="/person"><span>个人主页</span></router-link></li>
            </div>
            <div class="list">
                <img src="../../assets/购物车.png" />
                <li class="li1"><router-link to="/sample"><span>我的清单</span></router-link></li>
            </div>
        </div>
        <div class="crumbs">
          <nav>
              <router-link to="/home1"><span>首页</span><span>/</span></router-link>
               <router-link to="/home1"><span>{{ staName}}</span><span>/</span></router-link>
               <router-link :to="{
                 name: 'Home2', 
                 params: {
          param:this.staId,
          param1: this.staName,
               }
              }"><span>{{ pjName}}</span><span>/</span></router-link>
               <router-link :to="{
                 name: 'module', 
                 params: {
          staName: this.staName,
          pjName: this.pjName,
          pjId: this.pjId,
          staId:this.staId
               }
              }"><span>规程</span></router-link>
                <!-- <a href="#">首页</a><span>/</span>
                <a href="#">项目详情</a><span>/</span>
                <a href="#">法</a> -->
            </nav>
            
        </div>
    <div class="body">
        <div class="section">
            <div class="section-header">
                <h3>操作规程</h3>
                <div class="line"></div>
            </div>
            <el-tabs v-model="activeTab">
                <el-table 
                    :data="regulationsData" 
                    style="width: 100%"
                    :header-cell-style="tableHeaderStyle"
                    :cell-style="tableCellStyle">
                    <el-table-column 
                        prop="pgName" 
                        label="规程名称"
                        align="center">
                    </el-table-column>
                    <el-table-column 
                        label="操作" 
                        width="120"
                        align="center">
                        <template slot-scope="scope">
                            <el-button 
                                @click="downloadPdf(scope.row.pgId)" 
                                type="text" 
                                size="small"
                                class="download-btn">
                                <img src="../../assets/下载.png" class="download-icon"/>
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
        </div>
        <div class="section">
            <div class="section-header">
                <h3>比对方案</h3>
                <div class="line"></div>
            </div>
            <el-tabs v-model="activeTab">
                <el-table 
                    :data="schemeData" 
                    style="width: 100%"
                    :header-cell-style="tableHeaderStyle"
                    :cell-style="tableCellStyle"
                    empty-text="暂无对比方案">
                    <el-table-column 
                        v-if="!someProperty" 
                        prop="pgName"
                        label="方案名称"
                        align="center">
                    </el-table-column>
                    <el-table-column 
                        v-if="!someProperty"
                        label="操作"
                        width="120"
                        align="center">
                        <template slot-scope="scope">
                            <el-button 
                                @click="downloadPdf(scope.row.pgId)" 
                                type="text" 
                                size="small"
                                class="download-btn">
                                <img src="../../assets/下载.png" class="download-icon"/>
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column v-else>
                        <template>
                            <div class="empty-data">
                                <i class="el-icon-document"></i>
                                <span>暂无比对方案</span>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
        </div>
        <el-button 
            class="submit-btn" 
            type="primary"
            :disabled="someProperty"
            @click="submitApplication">
            提交申请
        </el-button>
    </div>

    <!-- 留言 -->
    <div class="chatbot">
        <img src="../../assets/客服.png" />
        <span class="chatbot-text">未找到所需内容？</span>
        <a href="#" class="chatbot-link" @click="toggleChatbot">点我留言</a>
    </div>

    <!-- 遮罩层 -->
    <div v-if="dialogVisible" class="chatbot-mask"></div>

    <!-- 留言弹出框 -->
    <div v-if="isChatbotVisible" class="chatbot-popup">
        <h2>留言板</h2>
        <textarea v-model="message" rows="4" cols="50" placeholder="请输入您的留言..."></textarea>
        <div class="popup-actions">
            <el-button type="primary" @click="submitMessage">提交留言</el-button>
            <el-button @click="toggleChatbot">关闭</el-button>
        </div>
    </div>
    </div>
</template>

<script>
import pdf from 'vue-pdf';
export default {
  components: { pdf },
  data() {
    return {
      someProperty:false,
      regulationsData: [
        // { regulationName: '操作规程1.pdf' },
        // { regulationName: '操作规程2.pdf' }
      ],
      schemeData: [
        // { schemeName: '对比方案.pdf' },
      ],
      staName:'',
      pjName:'',
      pjId:'',
      staId:'',
      pageSize: 10,
      currentPage: 1,
      pdfSrc: '',
      isChatbotVisible:false,
      dialogVisible:false,
      tableHeaderStyle: {
        backgroundColor: '#f5f7fa',
        color: '#606266',
        fontWeight: 500,
        height: '50px'
      },
      tableCellStyle: {
        padding: '12px 0',
        height: '50px'
      },
      activeTab: '0',
    };
  },
  created:function(){
    this.pjId = this.$route.params.pjId;
    this.pjName = this.$route.params.pjName;
    this.staName = this.$route.params.staName;
    this.staId = this.$route.params.staId;
    this.$http.get(`/findProgramByPjId/${this.pjId}`).then((response) => {
    this.schemeData=response.data;
    console.log(this.schemeData);
    if(this.schemeData.length == 0)
    {
      this.someProperty=true;
    }
  });
  this.$http.get(`/findPgByPjId/${this.pjId}`).then((response) => {
    this.regulationsData=response.data;
    console.log("this.regulationsData");
  });
  },
  methods: {
    downloadPdf(pgId) {
      console.log(pgId);
      this.$http.get(`/downloadProgram/${pgId}`).then((response) => {
        this.pdfSrc=response.data;
        fetch(this.pdfSrc)
          .then(response => response.blob())
          .then(blob => {
            const blobUrl = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = blobUrl;
            //link.download = 'regulation.pdf';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            window.URL.revokeObjectURL(blobUrl);
          })
          .catch(e => console.error('Error downloading the PDF:', e));
        })
      },
    submitApplication() {
      // 提交申请的逻辑
      console.log('提交申请');
      this.$http.put(`/addComparison/${this.pjId}`).then((response)=>{
        this.$alert(`${response.data}`, '提示', {
        confirmButtonText: '确定',
      });
      })
    },
   toggleChatbot() {
      this.isChatbotVisible = !this.isChatbotVisible; // 切换弹出框显示状态
   },
   submitMessage() {
        // 检查留言内容是否为空
        if (!this.message.trim()) {
          this.$message.error('留言内容不能为空');
          return;
        }

        // 直接发送字符串
        this.$http.post('/insertFeedback', this.message, {
          headers: {
            'Content-Type': 'text/plain'
          }
        }).then((response) => {
          // 成功提交后的处理
          this.isChatbotVisible = false; // 隐藏弹出框
          this.message = ''; // 清空留言内容

          // 显示成功提示
          this.$alert('留言成功', '提示', {
            confirmButtonText: '确定',
            type: 'success'
          });
        }).catch((error) => {
          // 处理错误情况
          console.error('留言提交失败:', error);
          
          // 根据错误状态码和响应内容给出相应提示
          if (error.response && error.response.status === 500) {
            this.$message.error('留言提交失败，请检查是否已登录');
          } else {
            this.$message.error('留言提交失败，请重试');
          }
        });
      },
  }
};
</script>

<style scoped>
.container {
    width: 100%;
    margin: 0 auto;
    font-family: Arial, sans-serif;
  }

  .head {
    width: 100%;
    background-color: #abe0f3;
    height: 6vh;
  }

    .head .img {
      height: 100%;
      float: left;
    }

      .head .img img {
        height: 100%;
      }

    .head .list {
      float: right;
      height: 45%;
    }

      .head .list img {
        margin-top: 15px;
        margin-right: 7px;
        height: 100%;
        float: left;
      }

      .head .list li {
        margin-top: 20px;
        margin-right: 40px;
        display: block;
        float: left;
        font-size: 18px;
      }

    .head .person {
      float: right;
      height: 42%;
    }

      .head .person img {
        margin-top: 15px;
        margin-right: 7px;
        height: 100%;
        float: left;
      }

      .head .person li {
        margin-top: 20px;
        margin-right: 70px;
        display: block;
        float: left;
        font-size: 18px;
      }


  .nav a {
    margin-left: 10px;
    text-decoration: none;
    color: #000;
  }

    .nav a.active {
      background-color: #555;
      color: #fff;
      padding: 5px 10px;
      border-radius: 5px;
    }

  .crumbs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #f0f0f0;
  }
  .back-button {
  position: fixed; /* 固定定位 */
  right: 5vw; /* 距离右边 20px */
  bottom: 5vh; /* 距离底部 20px */
  z-index: 1000; /* 确保按钮在最上层 */
}

    .back-button img {
      height: 60px;
      cursor: pointer; /* 当鼠标悬停时显示为手型 */
    }
.body {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.section {
    width: 90%;
  margin: 0 auto;
  padding: 0; /* 移除容器的内边距 */
}
.el-tabs {
  width: 100%; /* 确保标签页宽度占满容器 */
}

.el-table {
  width: 100%; /* 表格宽度占满容器 */
  max-width: 100%; /* 防止表格超出屏幕宽度 */
  margin-top: 20px; /* 表格顶部外边距 */
}

.el-table-column {
  text-align: center; /* 列内容居中对齐 */
}

.el-button {
  display: inline-block; /* 使按钮内联图标和文字在一行显示 */
}

.section img{
  width: 20px;
  height: 20px;
}

.body .submit{
  margin-top: 100px;
  width: 10vw;
  height: 5vh;
  /* background-color: dodgerblue; */
}
.chatbot {
    position: fixed;
    bottom: 20px;
    left: 20px;
    background-color:#f9f9f9 ;/* */
    padding: 10px;
    border-radius: 5px;
    display: flex;
    align-items: center; /* 垂直居中对齐 */
  }
.chatbot img{
    width: 70px;
}
.chatbot-text {
    margin-right: 10px; /* 与链接间距 */
}
.chatbot-link {
    color: black; /* 设置链接颜色为黑色 */
    text-decoration: underline; /* 添加下划线 */
    font-size: 16px; /* 设置字体大小 */
    transition: color 0.3s; /* 平滑颜色变化 */
}

.chatbot-link:hover,
.chatbot-link:focus,
.chatbot-link:active {
    color: black; /* 悬停、聚焦、点击时颜色不变 */
    text-decoration: underline; /* 保持下划线 */
}
.chatbot-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5); /* 灰色半透明遮罩 */
    z-index: 999; /* 遮罩层在弹出框下一层 */
 }
 .chatbot-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
    z-index: 1000; /* 确保弹出框在最上层 */
  }
  .popup-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
  }

  .chatbot-popup h2 {
    margin-top: 0; /* 移除标题的上边距 */
  }

  .chatbot-popup textarea {
    width: 100%;
    margin-bottom: 10px;
  }
  
.section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  padding: 20px;
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h3 {
  font-size: 18px;
  color: #303133;
  margin: 0;
  padding-right: 12px;
}

.line {
  flex: 1;
  height: 1px;
  background: linear-gradient(to right, #e0e0e0, transparent);
}

.download-btn {
  padding: 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.download-btn:hover {
  background: rgba(64, 158, 255, 0.1);
}

.download-icon {
  width: 20px;
  height: 20px;
  vertical-align: middle;
}

.empty-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #909399;
  padding: 32px 0;
}

.empty-data i {
  font-size: 48px;
  margin-bottom: 12px;
}

.submit-btn {
  width: 120px;
  height: 40px;
  margin: 20px auto;
  display: block;
}

:deep(.el-table) {
  border-radius: 4px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa !important;
}

:deep(.el-table--enable-row-hover .el-table__body tr:hover > td) {
  background-color: #f5f7fa;
}

:deep(.el-tabs__nav-wrap::after) {
  display: none;
}

:deep(.el-table__empty-text) {
  color: #909399;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .section {
    padding: 15px;
  }
  
  .section-header h3 {
    font-size: 16px;
  }
  
  .submit-btn {
    width: 100%;
  }
}
</style>